package com.lx.huimail.fragment;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.google.gson.Gson;
import com.lx.huimail.R;
import com.lx.huimail.activity.p_status_act.WaitGetGoodsActivity;
import com.lx.huimail.adapter.OrderManagerAdapter;
import com.lx.huimail.configs.Config;
import com.lx.huimail.utils.PullToRefreshView;
import com.lx.huimail.utils.PullToRefreshView.OnFooterRefreshListener;
import com.lx.huimail.utils.PullToRefreshView.OnHeaderRefreshListener;
import com.lx.huimail.weight.ActionItem;
import com.lx.huimail.weight.TitlePopup;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Pattern;

import okhttp3.Call;

public class Page2 extends Fragment implements OnHeaderRefreshListener, OnFooterRefreshListener{
    private PullToRefreshView mPullToRefreshView;
    private TitlePopup titlePopup;
    private TextView iv_show_Pop;
    private EditText etSearch;
    private ImageView search;
    public static RecyclerView recyclerView;
    public static OrderManagerAdapter orderManagerAdapter;
    public static String custid="";
    public static FragmentActivity instance;
    private LocationClient mLocClient;
    private String province, city, district;
    private String  latitude, lontitude;
    public static String TAG = "LocTestDemo";
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_2, container, false);
        instance=getActivity();
        recyclerView=(RecyclerView)view.findViewById(R.id.recyclerview);
        mPullToRefreshView = (PullToRefreshView) view.findViewById(R.id.main_pull_refresh_view);
        mPullToRefreshView.setOnHeaderRefreshListener(this);
        mPullToRefreshView.setOnFooterRefreshListener(this);
        getOrderInfo();
        return view;
    }
    public static void getOrderInfo(){

        HashMap<String,String> map=new HashMap<>();
        map.put("senderid",custid);
        map.put("sendstatus","0");
        map.put("page","1");
        map.put("rows","40");
        Gson gson=new Gson();
        String parmas=gson.toJson(map);
        OkHttpUtils
                .post()
                .url(Config.url+"order/searchOrder.do")
                .addParams("data",parmas)
                .addParams("mobile","true")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {

                        ArrayList<HashMap<String,String>> mList=new ArrayList<HashMap<String, String>>();
                        JSONArray jsonArray=null;
                        JSONObject jsonObject=null;
                        try {
                            jsonArray=new JSONArray(response);
                            for(int i=0;i<jsonArray.length();i++){
                                HashMap<String,String> map=new HashMap<String, String>();
                                map.put("orderno",jsonArray.getJSONObject(i).getString("orderno"));
                                map.put("createtime",jsonArray.getJSONObject(i).getString("createtime"));
                                map.put("sendstatus",jsonArray.getJSONObject(i).getString("sendstatus"));
                                map.put("ordercount",jsonArray.getJSONObject(i).getString("ordercount"));
                                map.put("ordermoney",jsonArray.getJSONObject(i).getString("ordermoney"));
                                map.put("custname",jsonArray.getJSONObject(i).getString("custname"));
                                map.put("receiveraddr",jsonArray.getJSONObject(i).getString("receiveraddr"));
                                map.put("warnflag",jsonArray.getJSONObject(i).getString("warnflag"));
                                map.put("name",jsonArray.getJSONObject(i).getString("name"));
                                map.put("array",String.valueOf(jsonArray.getJSONObject(i).getJSONArray("prolist")));
                                if (!jsonArray.getJSONObject(i).getString("online1").equals("null")){
                                    map.put("online1",jsonArray.getJSONObject(i).getString("online1"));
                                }else {
                                    map.put("online1","1");
                                }


                                mList.add(map);

                            }
                            orderManagerAdapter=new OrderManagerAdapter(instance,mList);
                            recyclerView.setLayoutManager(new LinearLayoutManager(instance));
                            recyclerView.setAdapter(orderManagerAdapter);
                            orderManagerAdapter.notifyDataSetChanged();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                });
    }
    @Override
    public void onFooterRefresh(PullToRefreshView view) {
        mPullToRefreshView.postDelayed(new Runnable() {

            @Override
            public void run() {
                handler.sendEmptyMessage(1);

                mPullToRefreshView.onFooterRefreshComplete();
            }
        }, 2000);
        getOrderInfo();
    }

    @Override
    public void onHeaderRefresh(PullToRefreshView view) {
        mPullToRefreshView.postDelayed(new Runnable() {

            @Override
            public void run() {
                handler.sendEmptyMessage(1);
                mPullToRefreshView.onHeaderRefreshComplete();
            }
        }, 2000);
        getOrderInfo();

    }
	/*-----------刷新接口方法实现-------	*/

    Handler handler = new Handler() {
        public void handleMessage(Message message) {
            super.handleMessage(message);
            mPullToRefreshView.onFooterRefreshComplete();

        }
    };
}
